<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>

<div class="row" >
	<div class="col-md-12">
		<div>
			<div class="box-header with-border">
				<h3 class="box-title">
    			
    			</h3>
    			<div class="box-tools pull-right" id="dataDicItemTreeTool_${uuid}">
    				<shiro:hasPermission name="DATADIC_ITEM_ORG_MOD">
		    		<button class="btn btn-sm btn-success addBt">
		    			<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 关联
		    		</button>
		    		</shiro:hasPermission>
		    		
		    		<shiro:hasPermission name="DATADIC_ITEM_ORG_DEL">
		    		<button class="btn btn-sm btn-danger delBt">
		    			<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 移除
		    		</button>
		    		</shiro:hasPermission>
    			</div>
			</div>
			<div class="box-body">
				<div id="dataDicItemTree_${uuid}"  class="ztree am-margin">

        		</div>
			</div>
		</div>
	</div>
   
</div>

<script type="text/javascript">
$(function () {
    var uuid = '${uuid}';
    var dataDicItemTree = $("#dataDicItemTree_"+uuid);
    var dataDicItemTreeTool = $("#dataDicItemTreeTool_"+uuid);
    var dataDicCode='${dataDic.code}';
    var orgId='${orgId}';
    
    dataDicItemTree = $.fn.zTree.init(dataDicItemTree, {
        treeId: "dataDicItemTree",
        async: {
            enable: true,
            url: ctx + "/dataDicAction/queryDicItemTreeForOrg.do",
            autoParam: ["id=pid"],
            otherParam: {dataDicCode:'${dataDic.code}',orgId:orgId},
            dataFilter: function (treeId, parentNode, responseData) {
                if (responseData) {
                    $.each(responseData, function (i, n) {
                        if (n.childrenSize > 0) {
                            n.isParent = true;
                        }
                    });
                }
                return responseData;
            }
        },
        data: {
            key: {
                name: "text"
            }
        },
        edit: {
            enable: true,
            showRemoveBtn: false,
            showRenameBtn: false,
            drag: {
                isCopy: false
            }
        },
        callback: {
            beforeAsync: function (treeId, treeNode) {
                //return !!dataDicItemTree.dataDicId;
            },
            onDrop: function (e, treeId, treeNodes, targetNode, moveType, isCopy) {
                if (treeNodes && treeNodes.length > 0) {
                    var treeNode = treeNodes[0];
                    var pNode = dataDicItemTree.getNodeByTId(treeNode.parentTId);
                    var ids = [];
                    $.each(pNode.children, function (i, n) {
                        ids.push(n.id);
                    });
                    $.post(ctx + "/dataDicAction/seqNodes.do", {ids: ids.join(","), pid: pNode.id}, function (data) {
                        if (data.success) {
                        } else {
                            webUtils.error(data.msg);
                        }
                    }, "json");
                }
            }
        }
    }, [{
            text: "数据字典",
            id: "-1",
            isParent: true,
            open: true
        }]);
    dataDicItemTree.reAsyncChildNodes(dataDicItemTree.getNodesByParam("id", "-1")[0], "refresh");
    
    var editDataDicItem = function (params) {
        params = params || {};
        var t = [];
        if (params.id) {
            t.push("id=" + params.id);
        }
        if (params.pid) {
            t.push("pid=" + params.pid);
        }
        if (params.dataDicId) {
            t.push("dataDicId=" + params.dataDicId);
        }
        if (params.dataDicCode) {
            t.push("dataDicCode=" + params.dataDicCode);
        }
        if(params.orgId){
        	t.push("orgId="+orgId);
        }
        
        var dialog=webUtils.dialog({
       	 title:'编辑字典项',
       	 url:ctx + '/dataDicAction/dicItemEditForOrgView.do?' + t.join("&"),
       	 onremove:function () {
                if (params.onClose) {
                    params.onClose.call(null);
                }
            }
        });
        return dialog;
    };

    dataDicItemTreeTool.find(".addBt").click(function () {
    	webUtils.dataDicSelector({
    		dataDicCode:dataDicCode,
    		onSelect:function(rows){
    			var ids=[];
    			$.each(rows,function(i,n){
    				ids.push(n.id);
    			});
    			$.post(ctx+"/dataDicAction/saveDataDicItemOrg.do",{ids:ids.join(","),orgId:orgId},function(r){
    				if(r.success){
    					dataDicItemTree.reAsyncChildNodes(dataDicItemTree.getNodesByParam("id", "-1")[0], "refresh");
    				}else{
    					webUtils.error("关联失败");
    				}
    			},"json");
    		}
    	});
    });

    dataDicItemTreeTool.find(".delBt").click(function () {
        var nodes = dataDicItemTree.getSelectedNodes();
        if (nodes && nodes.length > 0) {
        	webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
				if(result){
					var node = nodes[0];
					
					if(node.orgId=='-1'){
		            	webUtils.alert("不允许删除共享的字典");
		            }else{
		            	var ids = [];
		                ids.push(node.id);
		                $.post(ctx + "/dataDicAction/delItemOrgByIds.do", {ids: ids.join(",")}, function (data) {
		                    if (data.success) {
		                    	dataDicItemTree.reAsyncChildNodes(dataDicItemTree.getNodesByParam("id", "-1")[0], "refresh");
		                    } else {
		                    	webUtils.error(data.msg);
		                    }
		                }, 'json');
		            }
				}
    		});
        } else {
        	webUtils.alert("请选择需要删除的");
        }
    });
});
</script>